package com.apes.autoparts.supplier.todo;

import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apestech.framework.util.StringUtil;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;
import org.springframework.util.StringUtils;

import javax.persistence.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/***
 * 供应商待办列表解析
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = SupplierPlatformTodoParse.CONST_TABLE_NAME)
public class SupplierPlatformTodoParse extends BaseModel {

    public static final String CONST_TABLE_NAME = "supplier_platform_todo_parse";


    @Id
    @Comment("流水码")
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "供应商数据解析待办结果")
    private String id;

    @OneToOne(optional = false)
    @JoinColumn(name = "todo_id", referencedColumnName = "ID")
    @Comment("待办")
    private SupplierPlatformTodo todo;

    @Comment("对照产地")
    @Column(name = "contrast_origin_place")
    private String contrastOriginPlace;

    @Column(name = "contrast_brand_id", length = 4000)
    @Comment("对照品牌")
    private String contrastBrand;

    @Comment("对照零件编码")
    @Column(name = "contrast_part_code")
    private String contrastPartCode;

    @Column(name = "contrast_category_id", length = 4000)
    @Comment("对照配件名称")
    private String contrastCategory;


    @Transient
    private Map contrastBrands;
    public Map getContrastBrands() {
        if (isWrite() || isLoad()) {
            contrastBrands = StringToMap(this.contrastBrand);
        }
        return contrastBrands;
    }
    public void setContrastBrands(Map map) {
        this.contrastBrands = map;
        if (!isWrite()) {
            this.contrastBrand = mapToString(this.contrastBrands);
        }
    }

    @Transient
    private Map contrastCategories;
    public Map getContrastCategories() {
        if (isWrite() || isLoad()) {
            contrastCategories = StringToMap(this.contrastCategory);
        }
        return contrastCategories;
    }
    public void setContrastCategories(Map map) {
        this.contrastCategories = map;
        if (!isWrite()) {
            this.contrastCategory = mapToString(this.contrastCategories);
        }
    }

    @Transient
    public Map StringToMap(String st) {
        Map map = new HashMap();
        if (StringUtil.isEmpty(st)) return null;
        String[] rows = st.split(";");
        for (String row : rows) {
            if (row.contains(",")) {
                map.put(StringUtils.split(row, ",")[0], StringUtils.split(row, ",")[1]);
            }
        }
        return map;
    }

    public String mapToString(Map map) {
        StringBuffer sb = new StringBuffer();
        Set set = map.keySet();
        for (Object key : set) {
            sb.append(key.toString());
            sb.append(",");
            sb.append(map.get(key.toString()));
            sb.append(";");
        }
        return sb.toString();
    }
}
